package com.kong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kong.constant.MessageConstant;
import com.kong.constant.RedisConstant;
import com.kong.entity.PageResult;
import com.kong.entity.QueryPageBean;
import com.kong.entity.Result;
import com.kong.pojo.Setmeal;
import com.kong.service.SetmealService;
import com.kong.utils.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author yang2013
 * @data 2020/10/11 13:02
 * 增删改时需要对redis中的hash 数据进行修改
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private SetmealService setmealService;
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        try {
            String originalFilename = imgFile.getOriginalFilename();
            String last = originalFilename.substring(originalFilename.lastIndexOf("."));
            String randomName = UUID.randomUUID().toString().replace("-", "");
            String destFileName = randomName + last;
            QiNiuUtils.uploadViaByte(imgFile.getBytes(), destFileName);
            //直接添加的图片，包含垃圾图片--最后与插入数据库后存入redis的地址做差值
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, destFileName);
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,destFileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @PostMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        try{
           return  setmealService.findByPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/add")
    public Result addSetmeal(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);
            Jedis jedis = jedisPool.getResource();
            jedis.sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    @GetMapping("/findCheckgroupIdSBySId")
    public Result findCheckGrooupIds(Integer id){
        try{
            List<Integer> integerList=setmealService.findCheckGroupIds(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,integerList);
        }catch (Exception e){
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @PostMapping("/edit")
    public Result editSetmeal(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        try{
            Setmeal byId = setmealService.findById(setmeal.getId());
            setmealService.updateSetmeal(setmeal,checkgroupIds);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    @GetMapping("/delete")
    public Result deleteSetmeal(Integer id){
        try {
            Setmeal byId = setmealService.findById(id);
            setmealService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        }catch (RuntimeException r){
            return new Result(false, r.getMessage());
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }

    /**
     * 根据套餐id查询套餐对象
     */
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(Integer setmealId){
        try {
            Setmeal setmeal = setmealService.findById(setmealId);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    /**
     * 查询所有套餐
     */
    @GetMapping(value = "/findAll")
    public Result findAll(){
        try {
            List<Setmeal> setmeals = setmealService.findAll();
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmeals);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }


}
